using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ChatClient;
using SmartBoard.BackGround.BoardElement;
using SmartBoard.Book.Model;
using SmartBoard.Core.Command;
using SmartBoard.Core.Element;
using SmartBoard.Core.Networking.Client;
using SmartBoard.Server;
using System.IO;
using System.Net;

namespace SmartBoard
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private readonly SmartBoardServer _server;
        private readonly ClientConnectionManager _connManager;

        public Window1()
        {
            InitializeComponent();

            _connManager = new ClientConnectionManager();
            InitializeModule(this.background);

            (new BookCommandBinder(this)).Bind();
            var writer = new TextBoxTextWriter(this.txtBox);
            _server = new SmartBoardServer(writer, writer);
        }

        private void InitializeModule(IBoardElement element)
        {
            // Register Network Module.
            if (element.NetworkServiceModule != null)
            {
                _connManager.ModuleRegistrationService.RegisterModule(element.NetworkServiceModule);
            }

            //Bind Commands and Create a Menu Handler.
            var componentMenu = new MenuItem() {Header = element.GetType().Name};
            if(element.CommandManager != null)
            {
                var uicommands = from command in element.CommandManager.Commands where command is IUIBinderCommand select command as IUIBinderCommand;
                foreach (var command in uicommands)
                {
                    var item = new MenuItem();
                    command.RegisterWidget(item, WPFCommandInitializer.InitializeMenuItem, WPFCommandUIModifier.EnableDisableMenuItem);
                    componentMenu.Items.Add(item);
                }
            }
            this.componentsMenu.Items.Add(componentMenu);
        }


        private void HandleConnect(object sender, RoutedEventArgs e)
        {
            _server.Start();
        }

        private void HandleDisconnect(object sender, RoutedEventArgs e)
        {
            _server.Stop();
        }

        private void HandleCreateClient(object sender, RoutedEventArgs e)
        {
            (new frmClient()).Show();
        }

        private void HandleBackgroundElementColorChanged(object sender, RoutedEventArgs e)
        {
            this.background.ChangeBackgroundColor();
            var stream = new MemoryStream();
            this.background.StreamManager.WriteToDataStream(stream);
            var data = stream.ToArray();
            //_client.Write(data);
        }

        private void HandleCreateClientConnection(object sender, RoutedEventArgs e)
        {
            _connManager.Connection.Connect(IPAddress.Parse("127.0.0.1"), 8090);
        }
    }
}
